package mycsveditor.editors;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.rules.EndOfLineRule;
import org.eclipse.jface.text.rules.IPredicateRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.ITokenScanner;
import org.eclipse.jface.text.rules.RuleBasedPartitionScanner;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.ui.texteditor.ITextEditor;

public class CSVConfiguration extends SourceViewerConfiguration {

	private ColorManager fColorManager;

	private ITextEditor fTextEditor;

	private JavaAnalysisUtil fJavaAnalysisUtil;

	public CSVConfiguration(ColorManager colorManager) {
		this.fColorManager = colorManager;
	}

	public CSVConfiguration(ColorManager colorManager, ITextEditor editor) {
		super();
		this.fColorManager = colorManager;
		this.fTextEditor = editor;
		this.fJavaAnalysisUtil = new JavaAnalysisUtil(editor);
	}

	public ITextEditor getEditor() {
		return fTextEditor;
	}

	public void setEditor(ITextEditor editor) {
		this.fTextEditor = editor;
	}

	public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
		return new String[] { IDocument.DEFAULT_CONTENT_TYPE,
				CSVPartitionScanner.CSV_COMMENT, CSVPartitionScanner.JAVA_CALL };
	}

	protected ITokenScanner getCSVCommentScanner() {
		RuleBasedPartitionScanner scanner = new RuleBasedPartitionScanner();
		IToken csvOperComment = new Token(new TextAttribute(fColorManager
				.getColor(ICSVColorConstants.CSV_OPER_COMMENT)));
		IPredicateRule[] rules = new IPredicateRule[1];
		EndOfLineRule operCommentRule = new EndOfLineRule("#@", csvOperComment);
		operCommentRule.setColumnConstraint(0);
		rules[0] = operCommentRule;
		scanner.setPredicateRules(rules);
		return scanner;
	}

	public IPresentationReconciler getPresentationReconciler(
			ISourceViewer sourceViewer) {
		PresentationReconciler reconciler = new PresentationReconciler();

		CSVDamagerRepairer ndr = new CSVDamagerRepairer(getCSVCommentScanner(),
				new TextAttribute(fColorManager
						.getColor(ICSVColorConstants.CSV_COMMENT)));
		reconciler.setDamager(ndr, CSVPartitionScanner.CSV_COMMENT);
		reconciler.setRepairer(ndr, CSVPartitionScanner.CSV_COMMENT);

		ndr = new CSVDamagerRepairer(new JavaCallScanner(fColorManager),
				new TextAttribute(fColorManager
						.getColor(ICSVColorConstants.JAVA_CALL)));
		reconciler.setDamager(ndr, CSVPartitionScanner.JAVA_CALL);
		reconciler.setRepairer(ndr, CSVPartitionScanner.JAVA_CALL);

		return reconciler;
	}

	@Override
	public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
		ContentAssistant assistant = new ContentAssistant();

		IContentAssistProcessor csvContentAssistProcessor = new CSVContentAssistProcessor(
				fJavaAnalysisUtil);
		assistant.setContentAssistProcessor(csvContentAssistProcessor,
				CSVPartitionScanner.JAVA_CALL);
		assistant.setContentAssistProcessor(csvContentAssistProcessor,
				IDocument.DEFAULT_CONTENT_TYPE);
		assistant.enableAutoActivation(true);
		assistant.setAutoActivationDelay(300);
		assistant.setInformationControlCreator(null);
		assistant
				.setProposalPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
		assistant
				.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_BELOW);
		return assistant;
	}

	@Override
	public IHyperlinkDetector[] getHyperlinkDetectors(ISourceViewer sourceViewer) {
		return new IHyperlinkDetector[] { new JavaCallHyperlinkDetector(
				fJavaAnalysisUtil) };
	}

}